package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle.bean.University;
import com.vehicle.form.UniversityForm;
import com.vehicle.mapper.UniversityMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.mapper.UserLogMapper;
import com.vehicle.service.UniversityService;
import com.vehicle.util.LogUtil;
import com.vehicle.vo.UniversityListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-05-14
 */
@Service
public class UniversityServiceImpl extends ServiceImpl<UniversityMapper, University> implements UniversityService {

    @Autowired
    UniversityMapper universityMapper;

    @Autowired
    UserLogMapper userLogMapper;


    @Override
    public Boolean addUniversity(String ip,Integer id,String code, String name) {
        University university = new University();
        university.setUniverCode(code);
        university.setUniverName(name);
        int b = universityMapper.insert(university);
        if(b ==1) {
            userLogMapper.insert(LogUtil.insertAdminInsert(id,"添加了学校【"+name+"]",ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertAdminInsert(id,"添加学校【"+name+"]",ip,"失败"));
        return false;
    }

    @Override
    public Boolean deleteUniversity(String ip,Integer id,Integer univerId) {
        int b = universityMapper.deleteById(univerId);
        System.out.println("在这里可以吗");
        if(b == 1){
            userLogMapper.insert(LogUtil.insertAdminDelete(id,"删除了学校【"+univerId+"]",ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertAdminDelete(id,"没有删除学校【"+univerId+"]",ip,"失败"));
        return false;
    }

    @Override
    public Boolean updateUniversity(String ip,Integer id,UniversityForm universityForm) {
        UpdateWrapper<University> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("univer_id",universityForm.getUniverId());
        updateWrapper.set("univer_code",universityForm.getUniverCode());
        updateWrapper.set("univer_name",universityForm.getUniverName());
        int b  = universityMapper.update(null,updateWrapper);
        if(b ==1){
            userLogMapper.insert(LogUtil.insertAdminUpdate(id,"修改学校信息",ip,"成功"));
            return true;
        }
        userLogMapper.insert(LogUtil.insertAdminUpdate(id,"没有成功修改学校信息",ip,"失败"));
        return false;
    }

    @Override
    public UniversityListVO getAllUniversity(Integer page, Integer size) {
        Page<University> infoPage = new Page<>(page,size);
        Page<University> universityPage = universityMapper.selectPage(infoPage,null);
        UniversityListVO universityListVO = new UniversityListVO();
        universityListVO.setSize(universityPage.getSize());
        universityListVO.setTotal(universityPage.getTotal());
        universityListVO.setContent(universityPage.getRecords());
        return universityListVO;
    }

    @Override
    public List<University> getListByNameLike(String univerName) {
        QueryWrapper<University> universityQueryWrapper = new QueryWrapper<>();
        universityQueryWrapper.like("univer_name",univerName);
        List<University> universities = universityMapper.selectList(universityQueryWrapper);
        return universities;
    }

    @Override
    public String getUniversityNameById(Integer univerId) {
        String name = universityMapper.getNameById(univerId);
        return name;
    }
}
